<%@ page language="java" pageEncoding="UTF-8" %>
<%@ page import="com.ncr.teradata.bireport.*" %>
<%@ page import="ChartDirector.*" %>
<%@ page import="java.util.List" %>
<%@ page import="java.math.BigDecimal" %>
<%@ page import="java.util.Enumeration" %>
<%@ page import="com.telecom.cms.dto.SysUser" %>
<%
	String cityCode = "";
	String countryCode = "";
	
	String campId = "";
	String campSeqId = "";
	String minDateId = "";
	String maxDateId = "";
	String userId = "";
	Enumeration e = request.getParameterNames();
	while (e.hasMoreElements())
	{
		String hdName = (String)e.nextElement();
		if (hdName.equals("campId"))
			campId = request.getParameter(hdName);
		if (hdName.equals("campSeqId"))
			campSeqId = request.getParameter(hdName);
		if (hdName.equals("minDateId"))
			minDateId = request.getParameter(hdName);
		if (hdName.equals("maxDateId"))
			maxDateId = request.getParameter(hdName);
		if (hdName.equals("userId"))
			userId = request.getParameter(hdName);
	}
	
	Report report = new Report("CAMP_KPI_AUDIT",cityCode,countryCode);
	SysUser user = (SysUser)request.getSession().getAttribute("User_Inst");
	if (user.getUserRole().equals("1"))
		report.addFilter("Team_Id",new String[]{String.valueOf(user.getTeamId())});
	if (user.getUserRole().equals("2"))
		report.addFilter("User_Id",new String[]{String.valueOf(user.getUserId())});
	if(!campId.equals(""))
		report.addFilter("Camp_Id",new String[]{campId});
	if(!campSeqId.equals(""))
		report.addFilter("Camp_Seq_Id",new String[]{campSeqId});
		
	if(minDateId.equals(""))
		minDateId = CommonFunc.moveToDate((String)report.getDimensionMaxValue("Date_Id"),"yyyyMMdd",-14);
	if(maxDateId.equals(""))
		maxDateId = (String)report.getDimensionMaxValue("Date_Id");
	if(!minDateId.equals("") && !maxDateId.equals(""))
		report.addFilter("Date_Id",new String[]{Report.BETWEEN,minDateId,maxDateId});
	report.addDimension(new String[]{"User_Name"});
	report.addMeasure(new String[]{"Call_Total","Inter_Total","Accp_Total","Fin_Total","Sum_Call_Total","Sum_Inter_Total","Sum_Accp_Total","Sum_Fin_Total"});
	report.addDeriveMeasure("受理量排名",Report.RANKING,new String[]{"Accp_Total"});
	report.addDeriveMeasure("有效接触率",Report.RATIO,new String[]{"Inter_Total","Call_Total"});
	report.addDeriveMeasure("成功受理率",Report.RATIO,new String[]{"Accp_Total","Inter_Total"});
	report.addDeriveMeasure("累计受理率",Report.RATIO,new String[]{"Sum_Accp_Total","Sum_Inter_Total"});
	report.addDeriveMeasure("累计竣工率",Report.RATIO,new String[]{"Sum_Fin_Total","Sum_Accp_Total"});
	report.addSubtotal(Report.BOTTOM);
	//String tableStr = report.buildTableData();
	List reportData2 = report.buildData();
	
	Object[] campIds = report.getDimensionValues("Camp_Id");
	Object[] campSeqIds = report.getDimensionValues("Camp_Seq_Id","Camp_Id",campId);
	Object[] dateIds = report.getDimensionValues("Date_Id");
	Object[] userIds = report.getDimensionValues("User_Id");
	
	Report userReport = new Report("CAMP_KPI_AUDIT",cityCode,countryCode);
	if (user.getUserRole().equals("1"))
		userReport.addFilter("Team_Id",new String[]{String.valueOf(user.getTeamId())});
	if (user.getUserRole().equals("2"))
		userReport.addFilter("User_Id",new String[]{String.valueOf(user.getUserId())});
	if(!campId.equals(""))
		userReport.addFilter("Camp_Id",new String[]{campId});
	if(!campSeqId.equals(""))
		userReport.addFilter("Camp_Seq_Id",new String[]{campSeqId});
	if(!userId.equals(""))
		userReport.addFilter("User_Id",new String[]{userId});
	if(!minDateId.equals("") && !maxDateId.equals(""))
		userReport.addFilter("Date_Id",new String[]{Report.BETWEEN,minDateId,maxDateId});
	userReport.addDimension(new String[]{"Date_Id"});
	userReport.addMeasure(new String[]{"Call_Total","Inter_Total","Accp_Total","Fin_Total","Sum_Call_Total","Sum_Inter_Total","Sum_Accp_Total","Sum_Fin_Total"});
	userReport.addDeriveMeasure("有效接触率",Report.RATIO,new String[]{"Inter_Total","Call_Total"});
	userReport.addDeriveMeasure("成功受理率",Report.RATIO,new String[]{"Accp_Total","Inter_Total"});
	userReport.addDeriveMeasure("累计竣工率",Report.RATIO,new String[]{"Sum_Fin_Total","Sum_Accp_Total"});
	List reportData = userReport.buildData();
	
	String[] chartLabels = new String[reportData.size()];
	double[][] chartDatas = new double[3][reportData.size()];
	String[] chartDSName = new String[]{"成功受理率","有效接触率","累计竣工率"};
	for (int i = reportData.size() - 1, j = 0; i >= 0; i--,j++){
		Object[] row = (Object[])reportData.get(i); 
		chartLabels[j] = ((String)row[0]).substring(4);
		chartDatas[0][j] = ((BigDecimal)row[5]).doubleValue();
		chartDatas[1][j] = ((BigDecimal)row[3]).doubleValue();
		chartDatas[2][j] = ((BigDecimal)row[11]).doubleValue();
	}
	XYChart chart = new XYChart(600, 180);
	chart.setDefaultFonts("??","Bold");
	chart.setPlotArea(60, 10, 450, 140,0xffffff, -1, -1, 0xc0c0c0, -1);
	chart.addLegend(505, 10, false, "", 9).setBackground(Chart.Transparent);
	chart.yAxis().setTitle("受理率").setFontSize(9);
	chart.yAxis().setLinearScale(0, 100, 10);
	chart.yAxis().setLabelFormat("{value}%");
	chart.xAxis().setLabels(chartLabels).setFontAngle(0);
	chart.xAxis().setLabelStep(1);
	LineLayer layer = chart.addLineLayer2();
	layer.setLineWidth(1);
	layer.setDataLabelFormat("{value|0}%");
	for (int i = 0; i < chartDSName.length; i++)
		layer.addDataSet(chartDatas[i], -1, chartDSName[i]).setDataSymbol(Chart.DiamondSymbol,7, -1);
	String chartURL = chart.makeSession(request, "camp_audit_daily_chart2");
	String imageMap = chart.getHTMLImageMap("", "","title='{xLabel}日 {dataSetName} : {value}%'");
%>

<html>
<head>
	<title>营销监控日分析</title>
	<link href="../css/style.css" rel="stylesheet" type="text/css"/>
    <link href="../css/font.css" rel="stylesheet" type="text/css">
<SCRIPT>
	function campIdChg(campId){
		document.location.href = "camp_audit_detail.jsp?campId=" + campId;
	}

	function campSeqIdChg(campSeqId){
		form1.submit();
	}
	
	function dateIdChg(dateId){
		form1.submit();
	}
	
	function userIdChg(userId){
		form1.submit();
	}
	
	function accpMonitor(userId){
		window.open("moniter.jsp?camp_id=<%= campId %>&camp_seq_id=<%= campSeqId %>&user_id=" + userId);
	}
	
	function exportData(spanId,fileName)
	{
	
		document.all.exportFileName.value = fileName+".xls";
	    exportExcel(document.getElementById(spanId).innerHTML,'../export');
		document.all.exportFileName.value=fileName;
	}
	
	function exportExcel(s,URL)
	{	
	    var oldAction=document.forms[0].action; 
		document.forms[0].exportContent.value="<META content=text/html; charset=GBK http-equiv=Content-Type/>"+s;
		document.forms[0].action=URL;
		document.forms[0].submit();	
		document.forms[0].action = oldAction;
	}
</SCRIPT>
</head>
<body>
<form name="form1" method="post" action="camp_audit_detail.jsp">
	<input type="hidden" name="exportFileName" value="" />
	<input type="hidden" name="exportContent"  value="" />
	<table width="95%" height="100%"  border="0" align="center" cellpadding="0" cellspacing="1" >
		<tr>
			<td valign='top' align='left' bgcolor="#FFFFFF"> 
				<table border=0 width="100%" class="tablestyle4" style='table-layout:' cellspacing='2' cellpadding='2' bordercolordark='#ffffff' bordercolorlight='#ffffff'>
					<tr class="fontblue" width="100%" align="left">
						<td><img src="../images/title_left.gif" width="10" height="10"></td>
						<td align='left' valign='center'>
                    		营销监控日分析：
                    	</td>
                    	<td align='left' valign='top'>
							营销活动：
                        	<select class="select" name="campId" style='width:100' onchange="campIdChg(this.value);">
								<option value="" <% if(campId.equals("")) out.print("selected"); %>>全部</option>
								<% for(int i = 0; i < campIds.length; i++){ %>
	    	                		<OPTION value="<%= (String)campIds[i] %>" <% if(((String)campIds[i]).equals(campId)) out.print("selected"); %>><%= report.getNameValueByID("Camp_Id","Camp_Name",(String)campIds[i]) %></OPTION>
	    	                	<% } %>
	    	            	</select>
                        </td>
						<td align='left' valign='top'>
							营销批次：
                        	<select class="select" name="campSeqId" style='width:120' onchange="campSeqIdChg(this.value);">
								<option value="" <% if(campSeqId.equals("")) out.print("selected"); %>>全部</option>
	    	                	<% for(int i = 0; i < campSeqIds.length; i++){ %>
	    	                		<OPTION value="<%= (String)campSeqIds[i] %>" <% if(((String)campSeqIds[i]).equals(campSeqId)) out.print("selected"); %>><%= report.getNameValueByID("Camp_Seq_Id","Camp_Seq_Name",(String)campSeqIds[i]) %></OPTION>
	    	                	<% } %>
	    	            	</select>
                        </td>
                        <td align='left' valign='top'>
                    		开始日期：
                        	<select class="select" name="minDateId" style='width:100' onchange="dateIdChg(this.value);">
	    	                	<% for(int i = dateIds.length - 1; i >= 0; i--){ %>
	    	                		<OPTION value="<%= (String)dateIds[i] %>" <% if(((String)dateIds[i]).equals(minDateId)) out.print("selected"); %>><%= (String)dateIds[i] %></OPTION>
	    	                	<% } %>
	    	            	</select>
                        </td>
                        <td align='left' valign='top'>
                    		结束日期：
	    	            	<select class="select" name="maxDateId" style='width:100' onchange="dateIdChg(this.value);">
	    	                	<% for(int i = dateIds.length - 1; i >= 0; i--){ %>
	    	                		<OPTION value="<%= (String)dateIds[i] %>" <% if(((String)dateIds[i]).equals(maxDateId)) out.print("selected"); %>><%= (String)dateIds[i] %></OPTION>
	    	                	<% } %>
	    	            	</select>
                        </td>
                        <td align="right" valign='center'>
                    		<img name="btnSubmit" src="../images/button_export.gif" width="63" height="21" onclick="exportData('spanId1','Export data');" class="box_button">
                    	</td>
                        <td align="right" valign='center' width='10%'>
                        	&nbsp;
                        </td>
					</tr>
				</table>
				<span id="spanId1">
				<table border=1 width="100%" class="tablestyle1" style='table-layout:fixed' cellspacing='0' cellpadding='0' bordercolordark='#ccffff' bordercolorlight='#9999cc'>
					<tr align="center">
						<td>外呼人员名称</td>
						<td>呼叫总数</td>
						<td>接触总数</td>
						<td>有效接触率</td>
						<td>受理总数</td>
						<td>成功受理率</td>
						<td>受理量排名</td>
						<td>竣工总数</td>
						<td>累计呼叫总数</td>
						<td>累计接触总数</td>
						<td>累计受理总数</td>
						<td>累计受理率</td>
						<td>累计竣工总数</td>
						<td>累计竣工率</td>
						
					</tr>
					<%
						for(int i = 0; i < reportData2.size(); i++) {
							Object[] row = (Object[])reportData2.get(i); 
					%>
					<tr align="right">
						<TD align="center"><%= (i == reportData2.size() - 1 ? "小计" : (String)row[0]) %></a></TD>
						<TD><%= (BigDecimal)row[1] %></TD>
						<TD><%= (BigDecimal)row[2] %></TD>
						<TD><%= (BigDecimal)row[3] %>%</TD>
						<TD><a href="javascript:accpMonitor('<%= report.getNameValueByID("User_Name","User_Id",(String)row[0]) %>');" style='text-decoration:none;color:blue'><%= (BigDecimal)row[4] %></a></TD>
						<TD><%= (BigDecimal)row[5] %>%</TD>
						<TD><%= (BigDecimal)row[6] %></TD>
						<TD><%= (BigDecimal)row[7] %></TD>
						<TD><%= (BigDecimal)row[8] %></TD>
						<TD><%= (BigDecimal)row[9] %></TD>
						<TD><%= (BigDecimal)row[10] %></TD>
						<TD><%= (BigDecimal)row[11] %>%</TD>
						<TD><%= (BigDecimal)row[12] %></TD>
						<TD><%= (BigDecimal)row[13] %>%</TD>
					</tr>
					<% } %>
				</table>
				</span>
				<%//= tableStr %>
				<table border=0 width="100%" class="tablestyle4" style='table-layout:' cellspacing='1' cellpadding='0' bordercolordark='#ffffff' bordercolorlight='#ffffff'>
					<tr class="fontblue" width="100%" align="left">
                    	<td align='left' valign='top'>
                    		<br/>
							外呼人员：
                        	<select class="select" name="userId" style='width:100' onchange="userIdChg(this.value);">
								<option value="" <% if(userId.equals("")) out.print("selected"); %>>全部</option>
								<% for(int i = 0; i < userIds.length; i++){ %>
	    	                		<OPTION value="<%= (String)userIds[i] %>" <% if(((String)userIds[i]).equals(userId)) out.print("selected"); %>><%= report.getNameValueByID("User_Id","User_Name",(String)userIds[i]) %></OPTION>
	    	                	<% } %>
	    	            	</select>
                        </td>
                        <td align="right" valign='center'>
                        	&nbsp;
                        </td>
					</tr>
				</table>
				<table border=1 width=100% class="style4" style='table-layout:' cellspacing='0' cellpadding='0' bordercolordark='#FFFFFF' bordercolorlight='#FFFFFF'>
					<tr align="left">
						<td width="100%">
							<img src='<%= response.encodeURL("getchart.jsp?"+chartURL) %>' usemap="#map2" border="0">
							<map name="map2"><%= imageMap %></map>
						</td>
					</tr>
				</table>
			</td>
		</tr>
	</table>
</form>
</body>
</html>


